<template>
  <div class="page-content ss">
    <div class="page-title">岗位列表</div>
    <div class="content-box">
      <div class="search">
        <div class="search-line big-name clearfix">
          <div class="fl clearfix input-box">
            <span class="name fl">所属企业编号：</span>
            <input class="fl" type="text" v-model="searchParams.code" placeholder="请输入" />
          </div>
          <div class="fl clearfix input-box">
            <span class="name fl">岗位名称：</span>
            <input class="fl" type="text" v-model="searchParams.name" placeholder="请输入" />
          </div>
          <div class="fl">
            <div class="btn check-btn" @click="selectAll">查询</div>
          </div>
        </div>
        <div class="search-line big-name clearfix">
          <div class="fl clearfix input-box">
            <span class="name fl">状态：</span>
            <a-select
              default-value="请选择"
              :value="searchParams.isPublish"
              @change="selectPublish"
            >
              <a-select-option value=""> 请选择 </a-select-option>
              <a-select-option value="0"> 未发布 </a-select-option>
              <a-select-option value="1"> 已发布 </a-select-option>
            </a-select>
          </div>
        </div>
      </div>
      <div class="tabel">
        <a-table
          :columns="columns"
          :data-source="data"
          rowKey="id"
          :pagination="pagination"
          @change="handleTableChange"
        >
          <span slot="ageRequire" slot-scope="record">
            <span class="">{{record.ageRequire || '-'}}</span>
          </span>
          <span slot="otherRequire" slot-scope="record">
            <span class="">{{record.otherRequire || '-'}}</span>
          </span>
          <span slot="isPublish" slot-scope="text, record">
            <span class="state no-auth" v-if="!record.isPublish">未发布</span>
            <span class="state ing-auth" v-else>已发布</span>
          </span>
          <span slot="handler" slot-scope="record">
            <a
              class="handler-item"
              v-if="record.isPublish"
              href="javascript:void(0);"
              @click="toDetail(record, 1)"
              >详情</a
            >
            <a
              class="handler-item"
              v-if="!record.isPublish"
              href="javascript:void(0);"
              @click="toEdit(record, 2)"
              >编辑</a
            >
            <a
              class="handler-item"
              v-if="!record.isPublish"
              href="javascript:void(0);"
              @click="toPublish(record.id)"
              >发布</a
            >
            <a
              class="handler-item"
              v-if="!record.isPublish"
              href="javascript:void(0);"
              @click="deleteJob(record.id)"
              >删除</a
            >
            <a
              class="handler-item"
              v-if="record.isPublish"
              href="javascript:void(0);"
              @click="cancelPublish(record.id)"
              >取消发布</a
            >
          </span>
        </a-table>
      </div>
    </div>
    <a-modal v-model="visibleModel" destroyOnClose :width="720" :title="modelTitle" @ok="editOk">
      <div class="info">
        <div class="info-detail">
          <div class="info-item clearfix">
            <div class="front fl">
              <span class="dot">*</span>
              <span>所属企业: </span>
            </div>
            <div class="word fl">
              <input type="text" disabled maxlength="11" :value="editRecord.companyName" />
            </div>
          </div>
          <div class="info-item clearfix">
            <div class="front fl">
              <span class="dot">*</span>
              <span>岗位名称: </span>
            </div>
            <div class="word fl">
              <input
                :disabled="isEditOrDetail === 1"
                type="text"
                v-model="editRecord.name"
                placeholder="请输入"
              />
            </div>
          </div>
          <div class="info-item clearfix">
            <div class="front fl">
              <span class="dot">*</span>
              <span>薪资范围: </span>
            </div>
            <div class="word fl">
              <input
                :disabled="isEditOrDetail === 1"
                type="text"
                v-model="editRecord.salary"
                placeholder="请输入"
              />
            </div>
          </div>
          <div class="info-item clearfix">
            <div class="front fl">
              <span class="dot">*</span>
              <span>用工类型: </span>
            </div>
            <div class="word fl">
              <a-select
                :disabled="isEditOrDetail === 1"
                :defaultValue="editRecord.workerType"
                @change="editChangeWorkerType"
              >
                <a-select-option
                  :value="item.value"
                  v-for="item in workTypesSelect"
                  :key="item.text"
                >
                  {{ item.text }}
                </a-select-option>
              </a-select>
            </div>
          </div>
          <div class="info-item clearfix">
            <div class="front fl">
              <span class="dot">*</span>
              <span>性别要求: </span>
            </div>
            <div class="word fl">
              <a-select
                :disabled="isEditOrDetail === 1"
                :defaultValue="editRecord.gender"
                @change="editChangeGender"
              >
                <a-select-option :value="item.value" v-for="item in gendersSelect" :key="item.text">
                  {{ item.text }}
                </a-select-option>
              </a-select>
            </div>
          </div>
          <div class="info-item clearfix">
            <div class="front fl">
              <span class="dot">*</span>
              <span>学历要求: </span>
            </div>
            <div class="word fl">
              <a-select
                :disabled="isEditOrDetail === 1"
                :defaultValue="editRecord.education"
                @change="editChangeEducation"
              >
                <a-select-option
                  :value="item.value"
                  v-for="item in educationSelect"
                  :key="item.text"
                >
                  {{ item.text }}
                </a-select-option>
              </a-select>
            </div>
          </div>
          <div class="info-item clearfix">
            <div class="front fl">
              <span class="dot">*</span>
              <span>经验要求: </span>
            </div>
            <div class="word fl">
              <a-select
                :disabled="isEditOrDetail === 1"
                :defaultValue="editRecord.experience"
                @change="editChangeExperience"
              >
                <a-select-option
                  :value="item.value"
                  v-for="item in experiencesSelect"
                  :key="item.text"
                >
                  {{ item.text }}
                </a-select-option>
              </a-select>
            </div>
          </div>
          <div class="info-item clearfix">
            <div class="front fl">
              <!-- <span class="dot">*</span> -->
              <span>年龄要求: </span>
            </div>
            <div class="word fl">
              <input
                :disabled="isEditOrDetail === 1"
                type="text"
                v-model="editRecord.ageRequire"
                :placeholder="editRecord.ageRequire ? '请输入' : '-'"
              />
            </div>
          </div>
          <div class="info-item clearfix">
            <div class="front fl">
              <!-- <span class="dot">*</span> -->
              <span>其他要求: </span>
            </div>
            <div class="word fl">
              <input
                :disabled="isEditOrDetail === 1"
                type="text"
                v-model="editRecord.otherRequire"
                :placeholder="editRecord.otherRequire ? '请输入' : '-'"
              />
            </div>
          </div>
          <div class="info-item clearfix" v-if="isEditOrDetail == 2">
            <div class="front fl">
              <span class="dot">*</span>
              <span>发布设置: </span>
            </div>
            <div class="word fl">
              <a-radio-group
                :defaultValue="editIspublish"
                :options="isPublish"
                @change="radioChange"
              />
            </div>
          </div>
        </div>
      </div>
    </a-modal>
  </div>
</template>

<script>
const columns = [
  {
    title: '序号',
    dataIndex: 'no',
    key: 'no',
    align: 'center'
  },
  {
    title: '所属企业编号',
    dataIndex: 'code',
    align: 'center'
  },
  {
    title: '岗位名称',
    dataIndex: 'name',
    align: 'center',
    width: '10%'
  },
  {
    title: '薪资范围',
    dataIndex: 'salary',
    align: 'center',
    width: '10%'
  },
  {
    title: '用工类型',
    dataIndex: 'workerTypeDes',
    align: 'center',
    width: '10%'
  },
  {
    title: '性别要求',
    dataIndex: 'genderDes',
    align: 'center'
  },
  {
    title: '学历要求',
    dataIndex: 'educationDes',
    align: 'center'
  },
  {
    title: '经验要求',
    dataIndex: 'experienceDes',
    align: 'type',
    width: '10%'
  },
  {
    title: '年龄要求',
    // dataIndex: 'ageRequire',
    scopedSlots: { customRender: 'ageRequire' },
    align: 'center'
  },
  {
    title: '其他要求',
    // dataIndex: 'otherRequire',
    scopedSlots: { customRender: 'otherRequire' },
    align: 'center',
    width: '20%'
  },
  {
    title: '更新时间',
    dataIndex: 'updateTimeDes',
    align: 'center',
    width: '10%'
  },
  {
    title: '状态',
    // dataIndex: "isPublish",
    scopedSlots: { customRender: 'isPublish' },
    align: 'center'
  },
  {
    title: '操作',
    // key: "handler",
    align: 'center',
    scopedSlots: { customRender: 'handler' },
    width: '15%'
  }
]

export default {
  data() {
    return {
      data: [],
      columns,
      visibleModel: false,
      modelTitle: '',
      isEditOrDetail: 1, // 1-详情，2-编辑
      workTypesSelect: [],
      educationSelect: [],
      experiencesSelect: [],
      gendersSelect: [],
      editIspublish: '',
      isPublish: [
        { label: '立即发布', value: 1 },
        { label: '暂不发布', value: 0 }
      ],
      pagination: {
        showQuickJumper: true,
        current: 1,
        pageSize: this.$config.pageSize,
        total: 1,
        showTotal: (total, range) =>
          `共 ${total} 条记录 第${Math.ceil(range[0] / this.pagination.pageSize)} / ${Math.ceil(
            total / this.pagination.pageSize
          )} 页`
      },
      searchParams: {
        code: '',
        isPublish: '',
        name: '',
        pageNo: 1,
        pageSize: this.$config.pageSize
      },
      editRecord: {}
    }
  },
  mounted() {
    this.init()
    // 获取工作类型
    this.$http.get('/commons/work-types', { params: { isLimit: true } }).then((res) => {
      if (res.success) {
        this.workTypesSelect = res.model.workTypes
      }
    })
    // 获取教育程度
    this.$http.get('/commons/educations', { params: { isLimit: true } }).then((res) => {
      if (res.success) {
        this.educationSelect = res.model.educations
      }
    })
    // 获取教育程度
    this.$http.get('/commons/experiences', { params: { isLimit: true } }).then((res) => {
      if (res.success) {
        this.experiencesSelect = res.model.experiences.filter((item) => {
          return item.text !== '无经验'
        })
      }
    })
    // 获取性别类型
    this.$http.get('/commons/genders', { params: { isLimit: false } }).then((res) => {
      if (res.success) {
        this.gendersSelect  = res.model.genders
      }
    })
  },
  watch: {
    '$route.path': function(newVal, oldVal) {
      if (newVal !== oldVal) {
        this.init()
      }
    }
  },
  methods: {
    init() {
      this.searchParams.code = ''
      this.searchParams.isPublish = ''
      this.searchParams.name = ''
      this.searchParams.pageNo = ''
      this.searchParams.pageSize = this.$config.pageSize
      this.getList()
    },
    getList() {
      this.$http
        .get('/platforms/jobs', {
          params: this.searchParams
        })
        .then((res) => {
          if (res.success) {
            this.pagination.current = res.model.pageNo
            this.pagination.total = res.model.totalCount
            this.data = res.model.data.map((item, index) => {
              item.no = index + 1
              return item
            })
          } else {
            this.$message.error(res.message)
          }
        })
    },
    handleTableChange(pagination) {
      this.pagination.current = pagination.current
      this.pagination.pageSize = pagination.pageSize
      this.searchParams.pageNo = pagination.current
      this.getList()
    },
    selectPublish(value) {
      this.searchParams.pageNo = 1
      this.searchParams.isPublish = value
      this.getList()
    },
    selectAll() {
      this.searchParams.pageNo = 1
      this.getList()
    },
    // 发布
    toPublish(id) {
      this.$http
        .put('/platforms/jobs/publish-statuses/' + id, { isPublish: 1 })
        .then((res) => {
          if (res.success) {
            this.getList()
            this.$message.success('发布成功！')
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(() => {
          this.$message.error('发布失败')
        })
    },
    // 取消发布
    cancelPublish(id) {
      this.$http
        .put('/platforms/jobs/publish-statuses/' + id, { isPublish: 0 })
        .then((res) => {
          if (res.success) {
            this.getList()
            this.$message.success('取消发布成功！')
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(() => {
          this.$message.error('取消发布失败')
        })
    },
    // 删除岗位
    deleteJob(id) {
      this.$confirm({
        title: '提示',
        content: '确认删除此岗位',
        onOk: () => {
          this.$http
            .delete('/platforms/jobs/' + id)
            .then((res) => {
              if (res.success) {
                this.getList()
                this.$message.success('删除成功')
              } else {
                this.$message.error('删除失败')
              }
            })
            .catch(() => {
              this.$message.error('删除失败')
            })
        }
      })
    },
    // 详情
    toDetail(record, type) {
      this.modelTitle = '岗位详情'
      this.isEditOrDetail = type
      this.editRecord.id = record.id
      this.editRecord.companyName = record.companyName
      this.editRecord.name = record.name
      this.editRecord.salary = record.salary
      this.editRecord.gender = record.gender
      this.editRecord.genderDes = record.genderDes
      this.editRecord.workerType = record.workerType
      this.editRecord.workerTypeDes = record.workerTypeDes
      this.editRecord.education = record.education
      this.editRecord.educationDes = record.educationDes
      this.editRecord.experience = record.experience
      this.editRecord.experienceDes = record.experienceDes
      this.editRecord.ageRequire = record.ageRequire
      this.editRecord.otherRequire = record.otherRequire
      this.visibleModel = true
    },
    // 编辑
    toEdit(record, type) {
      this.modelTitle = '编辑岗位'
      this.isEditOrDetail = type
      this.editRecord.id = record.id
      this.editIspublish = 1
      this.editRecord.companyName = record.companyName
      this.editRecord.name = record.name
      this.editRecord.salary = record.salary
      this.editRecord.gender = record.gender
      this.editRecord.genderDes = record.genderDes
      this.editRecord.workerType = record.workerType
      this.editRecord.workerTypeDes = record.workerTypeDes
      this.editRecord.education = record.education
      this.editRecord.educationDes = record.educationDes
      this.editRecord.experience = record.experience
      this.editRecord.experienceDes = record.experienceDes
      this.editRecord.ageRequire = record.ageRequire
      this.editRecord.otherRequire = record.otherRequire
      this.visibleModel = true
    },
    editChangeWorkerType(value) {
      this.editRecord.workerType = value
    },
    editChangeEducation(value) {
      this.editRecord.education = value
    },
    editChangeExperience(value) {
      this.editRecord.experience = value
    },
    editChangeGender(value) {
      this.editRecord.gender = value
    },
    radioChange(res) {
      this.editIspublish = res.target.value
    },
    editOk() {
      if (this.isEditOrDetail === 1) {
        this.visibleModel = false
      }
      if (this.isEditOrDetail === 2) {
        let params = Object.assign(this.editRecord, { isPublish: this.editIspublish })
        this.$http.put('/platforms/jobs/' + this.editRecord.id, params).then((res) => {
          if (res.success) {
            this.visibleModel = false
            this.getList()
            this.$message.success('保存成功')
          } else {
            this.$message.error(res.message)
          }
        })
      }
    }
  }
}
</script>

<style lang="less">
.ss .ant-table td {
  white-space: nowrap;
  max-width: 120px;
}
</style>
